package com.wolfsea.windowmanagerdemo;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuliheng
 * @desc WindowViewBuilder
 * @time 2022/4/17  11:06
 **/
public class WindowViewBuilder2 {

    private WindowView2 windowView2;
    private WindowManager windowManager;

    public void show() {
        if (windowManager != null) {
            try {
                windowManager.addView(windowView2, initLayoutParam());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        windowView2.findViewById(R.id.body).setOnClickListener(view -> windowView2.hide(false));
    }

    private WindowManager.LayoutParams initLayoutParam() {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams
                (WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT,
                        0, 0,
                        PixelFormat.TRANSPARENT
                );
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.gravity = Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR;
        layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
        return layoutParams;
    }

    private void setActivity(AppCompatActivity activity) {
        windowView2 = new WindowView2(activity);
        windowManager = activity.getWindowManager();
    }

    public static class WindowViewBuilder2In {

        private static final Map<String, WindowViewBuilder2> windowMap = new HashMap<>();

        public static WindowViewBuilder2 create(AppCompatActivity activity){
            WindowViewBuilder2 builder2 = new WindowViewBuilder2();
            builder2.setActivity(activity);
            new Handler(Looper.getMainLooper()).post(() -> {
                WindowViewBuilder2 windowViewBuilder2 = windowMap.get(activity.toString());
                if (windowViewBuilder2 != null && windowViewBuilder2.windowView2 != null) {
                    if (windowViewBuilder2.windowView2.isAttachedToWindow()) {
                        ViewCompat.animate(windowViewBuilder2.windowView2).alpha(0).withEndAction(() -> {
                            if (windowViewBuilder2.windowView2.isAttachedToWindow()) {
                                activity.getWindowManager().removeViewImmediate(windowViewBuilder2.windowView2);
                            }
                        });
                    }
                }
                windowMap.put(activity.toString(), builder2);
            });
            return builder2;
        }
    }

}
